package com.joezhou.spring.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author JoeZhou
 */
@Slf4j
@Component
public class UserAspectByXml {

    public Object aroundAdvice(ProceedingJoinPoint pjp) {
        int[] result = null;
        try {
            log.info("前置通知：登录鉴权...");

            // pjp.getArgs()：获取业务方法的形参
            // pjp.proceed()：调用业务方法
            result = (int[]) pjp.proceed(pjp.getArgs());
        } catch (Throwable e) {
            log.error("异常通知：爆发异常..." + e);
        } finally {
            log.info("后置通知：记录日志...");

            // 改变返回值
            if (result != null) {
                log.info("返回后通知：将原返回值 " + Arrays.toString(result) + " 改为 [4, 5, 6]...");
                result[0] = 4;
                result[1] = 5;
                result[2] = 6;
            }
        }
        return result;
    }
}